import { Room } from "../GOBE/GOBE";
import { Global } from "./Global";
import LaunchParam from "./LaunchParam";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.EditBox)
    editBoxRoomId: cc.EditBox = null;

    @property
    text: string = 'hello';

    start() {
        // init logic
        this.label.string = this.text;
        let playerId = LaunchParam.Inst().LaunchParam["playerid"];
        Global.Inst().Init(playerId);
    }

    OnCreatRoomBtnClick() {
        Global.Inst().CreateRoom().then((room: Room) => {
            console.warn('创建房间成功', room);
            this.label.string = room.roomId;
            this.editBoxRoomId.string = room.roomId;
        });
    }

    OnJoinRoomBtnClick() {
        this.editBoxRoomId.string = this.editBoxRoomId.string.trim();
        Global.Inst().JoinRoom(this.editBoxRoomId.string).then((room: Room) => {
            console.warn('创建房间成功', room);
            this.label.string = room.roomId;
        });
    }
}
